package com.xywh.emergency_outbound.mvp.ui.util;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author 小星 QQ:753940262
 * @class describe
 * @time 2019/11/27 0027 10:20
 */
public class GPSManager {

    private static final String TAG = "GPSManager";
    private LocationManager mLocationManager;
    private Context mContext;
    private LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                Log.d(TAG, "onLocationChanged: 经纬度:" + latitude + "--" + longitude);
                Toast.makeText(mContext, "经纬度:" + latitude + "--" + longitude, Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "onStatusChanged: " + provider + "  " + status);
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, "onProviderEnabled: " + provider);
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG, "onProviderDisabled: " + provider);
        }
    };

    public GPSManager(Context context) {
        mContext = context;
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    }

    /**
     * 判断定位服务是否开启
     */
    public boolean isOpen() {
        boolean gps = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // WLAN或移动网络
        boolean network = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }
        return false;
    }

    /**
     * 打开定位服务
     */
    public void openGPS(Context context) {
        Intent GPSIntent = new Intent();
        GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvide");
        GPSIntent.addCategory("android.intent.category.ALTERNATIVE");
        GPSIntent.setData(Uri.parse("toast:3"));

        try {
            //使用PendingIntent发送广播告诉手机去开启GPS功能
            PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send();
        } catch (PendingIntent.CanceledException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取经纬度信息
     */
    public void getGPSConfi(Activity activity, TextView tv) {
        if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
            return;
        }
        Location location;
//        if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
//            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mLocationListener);
//            location = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//        } else {
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
        location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//        }

        if (location != null) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            Log.e(TAG, "经纬度:" + latitude + "--" + longitude);
            tv.setText("经纬度:" + latitude + "--" + longitude);
            Toast.makeText(activity, "经纬度:" + latitude + "--" + longitude, Toast.LENGTH_SHORT).show();

        } else {
            Log.e(TAG, "未获取到经纬度数据");
            Toast.makeText(activity, "未获取到经纬度数据", Toast.LENGTH_SHORT).show();
        }
    }
}